<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 头部 -->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="学科名称">
            <el-input v-model="formInline.user"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="clearInput">清空</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="searching"
              >搜索</el-button
            >
          </el-form-item>
          <!-- 右边按钮 -->
          <el-button
            type="success"
            size="small"
            style="float: right"
            @click="dialogVisible = true"
          >
            <i class="el-icon-edit"></i> 新增技巧</el-button
          >
        </el-form>
        <!-- 提示信息 -->
        <el-alert title="" type="info" show-icon>
          <span>数据一共 {{ total }} 条</span></el-alert
        >
        <!-- 表格 -->
        <el-table :data="list" style="width: 100%" stripe>
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="subjectName" label="学科名称" width="180">
          </el-table-column>
          <el-table-column prop="username" label="创建者" width="180">
          </el-table-column>
          <el-table-column label="创建日期" width="180">
            <template #default="{ row }">
              {{ formatDate(row.addDate) }}
            </template>
          </el-table-column>
          <el-table-column label="前台是否显示" width="180">
            <template #default="{ row }">
              {{ isDisplay[row.isFrontDisplay] }}
            </template>
          </el-table-column>
          <el-table-column prop="twoLevelDirectory" label="二级目录">
          </el-table-column>
          <el-table-column prop="tags" label="标签" width="180">
          </el-table-column>
          <el-table-column prop="totals" label="题目数量" width="180">
          </el-table-column>
          <el-table-column label="操作" width="300">
            <template #default="{ row }">
              <el-button
                type="text"
                @click="subjectClassify(row.id, row.subjectName)"
                >学科分类</el-button
              >
              <el-button
                type="text"
                @click="subjectLable(row.id, row.subjectName)"
                >学科标签</el-button
              >
              <el-button type="text" @click="showDialogEnt(row.id)"
                >修改</el-button
              >
              <el-button type="text" @click="deleteSubject(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-row type="flex" justify="end">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.page"
            :page-size="params.pagesize"
            :page-sizes="[5, 10, 20, 50]"
            layout="prev, pager, next,total, sizes,  jumper"
            :total="total"
          >
          </el-pagination>
        </el-row>
      </el-card>
      <!-- 新增弹框组件 -->
      <subjects-add
        :dialogVisible="dialogVisible"
        @close-dialog="closeDialog"
        @get-datalist="getDataList"
        :isId="isId"
      ></subjects-add>
    </div>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects.js'
// import subjectAdd from '@/module-hmmm/components/subject-add.vue'
import dayjs from 'dayjs'
import SubjectsAdd from '../components/subjects-add.vue'
export default {
  name: 'aaaa',
  components: { SubjectsAdd },
  data () {
    return {
      // 输入框双向绑定内容
      formInline: {
        user: ''
      },
      // 表格数据
      list: [],
      // 总页数
      total: 0,
      // 分页参数
      params: {
        page: 1,
        pagesize: 10
      },
      // 枚举
      isDisplay: {
        1: '是',
        0: '否'
      },
      dialogVisible: false,
      // // 表单内容
      // formModel: {
      //   subjectName: '',
      //   isFrontDisplay: '' // 权限启用标识
      // },
      // 表单自定义校验规则
      formRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },
      // id
      isId: ''
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    // 点击页码发生变化
    handleCurrentChange (newPage) {
      this.params.page = newPage
      // 获取表单列表数据
      this.getDataList()
    },
    // 点击每页显示几个数据
    handleSizeChange (newSize) {
      this.params.pagesize = newSize
      // 获取表单列表数据
      this.getDataList()
    },
    // 获取学科列表
    async getDataList () {
      const res = await list(this.params)
      // console.log(res)
      this.list = res.data.items
      this.total = res.data.counts
    },
    // 转换时间
    formatDate (dateValue) {
      return dayjs(dateValue).format('YYYY-MM-DD HH:mm:ss')
    },
    // 清空搜索空内容
    clearInput () {
      this.formInline.user = ''
      this.getDataList()
    },
    // 点击搜索触发的事件
    async searching () {
      // console.log(1)
      this.params.page = 1
      const res = await list({
        ...this.params,
        // page: this.params.page,
        // pagesize: (this.params.pagesize = 100000),
        subjectName: this.formInline.user
      })
      // this.params.pagesize = 10
      // console.log(res)
      // this.params.page = 1
      // this.params.pagesize = 10
      this.list = res.data.items
      this.total = res.data.counts
    },
    // // 点击新增触发事件
    // newadditions () {
    //   this.dialogVisible = true
    // },
    // 点击X和蒙版关闭弹窗
    closeDialog () {
      this.dialogVisible = false
      // 清空表单内容
      // this.$refs.formRef.resetFields()
      // 手动清空表单内容
      // this.formModel = {
      //   subjectName: '',
      //   isFrontDisplay: '' // 权限启用标识
      // }
    },
    // // 点击确认提交添加
    // submit () {
    //   this.$refs.formRef.validate(async valid => {
    //     if (!valid) return
    //     // 校验成功
    //     if (this.formModel.id) {
    //       // 编辑
    //       await update(this.formModel)
    //     } else {
    //       // 添加
    //       await add({
    //         subjectName: this.formModel.subjectName,
    //         isFrontDisplay: this.formModel.isFrontDisplay
    //       })
    //     }
    //     // 提示用户
    //     const msg = this.formModel.id ? '编辑成功' : '添加成功'
    //     this.$message.success(msg)
    //     // 刷新表单数据
    //     this.getDataList()
    //     // 关闭弹框
    //     this.dialogVisible = false
    //   })
    // },
    // 点击修改触发的事件
    async showDialogEnt (id) {
      // 打开弹窗
      this.dialogVisible = true
      // 获取学科详细数据
      // const res = await detail(id)
      // console.log(res)
      // 数据回显
      // this.formModel = res.data
      // this.$refs.vcc.openDialog()
      this.isId = id
    },
    // 点击删除删除学科
    deleteSubject (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 调用接口
        const res = await remove(id)
        // console.log(res)
        if (res.data.error) {
          this.$message.error(res.data.error)
        } else {
          // 提示用户
          this.$message.success('删除学科成功')
          // 当某一页数据只有一条,并且当前页码值大于1时,要让页数-1
          if (this.list.length === 1 && this.params.page > 1) {
            this.params.page--
          }
          // 刷新页面
          this.getDataList()
        }
      })
    },
    // 跳转学科分类
    async subjectClassify (id, subjectName) {
      // const res = await simple(subjectName)
      // console.log(res.data)
      this.$router.push({
        path: 'directorys',
        query: {
          id: id,
          name: subjectName
          // query参数会拼接成  /xxxxx?参数=值&参数=值
        }
      })
    },
    // 跳转学科标签
    subjectLable (id, subjectName) {
      this.$router.push({
        path: 'tags',
        query: {
          id: id,
          name: subjectName
          // query参数会拼接成  /xxxxx?参数=值&参数=值
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-card {
  font-size: 14px;
  // 筛选 表单样式
  .inline {
    display: flex;
    justify-content: space-between;
  }
  // 表格样式
  .table {
    margin-bottom: 20px;
    .el-table::v-deep th {
      background-color: #fafafa;
    }
    .el-link {
      margin-right: 20px;
    }
  }
  // 分页
  .paging {
    float: right;
    margin-bottom: 20px;
  }
  // 弹框
  .dialog {
    .button {
      display: flex;
      justify-content: right;
      margin-top: 20px;
      margin-bottom: 0;
    }
  }
}
</style>
